home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / outbox.idb / var / www / cgi-bin / wrap.z / wrap
Encoding:
Text File  |  1996-11-14  |  20.3 KB  |  940 lines

  1. #!/usr/bin/perl
  2.  
  3. #__________________________________________________________
  4. #
  5. #    File:    wrap
  6. #    By:      Matt Ho
  7. #   Date:    7/23/95
  8. #   Purpose: Wrapper to dynamically create HTML documents
  9. #            that contain a easy to read directory content.
  10. #__________________________________________________________
  11.  
  12.  
  13. require "flush.pl";
  14.  
  15. #__________________________________________________________
  16. #
  17. #    Set some environment variables, we'll need through the
  18. #    script and do some initial error checking.
  19. #__________________________________________________________
  20.  
  21. $ROOT     = "/var/www/htdocs" ;    # Root directory
  22. $PATH     = $ENV{'PATH_INFO'} ; 
  23. $wrap     = "/cgi-bin/wrap" ;        # Script alias for this CGI
  24. $ls       = "/sbin/ls -a1" ;
  25. $outboxcgi = "/var/www/cgi-bin/outbox-cgi" ;
  26. #__________________________________________________________
  27. #
  28. #    Read the form data in.  PATH_INFO may be passed here
  29. #    if this was called from a SSI.  We're going to make
  30. #    one final tweak and read from the arg list as well
  31. #    for SSI's.
  32. #__________________________________________________________
  33.  
  34. if( $ENV{'REQUEST_METHOD'} eq "GET" )
  35. {
  36.    $buffer=$ENV{'QUERY_STRING'} ;
  37. else
  38. {
  39.    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}) ;
  40. }
  41.  
  42. @pairs = split(/&/, $buffer) ;
  43. foreach (@pairs)
  44. {
  45.     tr/+/ / ;    
  46.    ($name,$value) =  split(/=/) ;
  47.     $value        =~ s/%(..)/pack("c",hex($1))/ge ;
  48.     $name         =~ s/%(..)/pack("c",hex($1))/ge ;
  49.  
  50.     $FORM{$name} = $value ;
  51. }
  52.  
  53. #_________________________________
  54. #
  55. #    Some basic checks to take
  56. #    care of
  57. #_________________________________
  58.  
  59. if( $ARGV[0] )
  60. {
  61.     $PATH = $ARGV[0] ;
  62.     $arg  = 1 ;
  63. }
  64. chop $PATH if substr($PATH, -1) eq "/" ;
  65. @_        = split('/', $PATH) ;
  66. $pathRoot = $_[$#_] ;
  67. $doc      = $ROOT.$PATH ;
  68.  
  69. &DefaultMesg if ! defined $PATH || $PATH eq "" ;    # Get a base listing =)
  70. &ErrBadPath unless &ValidPath ;    # Check for server spoofing
  71. &ErrBadPath unless -e $doc ;    # Check to see it exists
  72. &HandleDownload if -f $doc ;    # Do the right thing
  73.  
  74. #__________________________________________________________
  75. #
  76. #    If we get to this point, we can be reasonably certain
  77. #    that this is a valid shared directory.  We want to
  78. #    show it using the appropriate method.  The following
  79. #    section is the leading data.  ShowContents will supply
  80. #    the rest of the data.
  81. #
  82. #    We do, however, need to account for the user putting
  83. #    an index.html file in the directory.  We'll handle 
  84. #    that as follows.  If the file exists, it is assumed to
  85. #    be in the right.  We will append out data at the end,
  86. #    using the index.html's head section. Otherwise, we
  87. #    will use ours.
  88. #__________________________________________________________
  89.  
  90. if( $arg == 1 )
  91. {
  92.     #    This is intentionally left blank
  93. }
  94. elsif( -r "$doc/index.html" )
  95. {
  96.     print "Content-type: text/html\n\n" ;
  97.  
  98.     open(INDEX, "$doc/index.html") ;
  99.     while( <INDEX> )
  100.     {
  101.         s/<!--#exec/<!-- exec/g ;
  102.         last if /<\/[bB][oO][dD][yY]>/ ;
  103.         last if /<\/[hH][tT][mM][lL]>/ ;
  104.         print ;
  105.         if ( /<BODY/ || /<body/)
  106.         {
  107.             if ( ! />/ )
  108.             {
  109.             while( <INDEX> )
  110.             {
  111.                 print ;
  112.                 last if ( />/ ) ;
  113.             }
  114.             }
  115.             &flush(STDOUT) ;
  116.             system '/var/www/cgi-bin/outbox-cgi', $PATH;
  117.         }
  118.     }
  119.     close(INDEX) ;
  120. }
  121. else
  122. {
  123.     print <<ENDOFTEXT ;
  124. Content-type: text/html
  125.  
  126. <head>
  127.     <title>
  128.         $pathRoot
  129.     </title>
  130. </head>
  131.  
  132. <body bgcolor="c1c1c1" text="000000" link="0000ee" vlink="0000ee"
  133. alink="0000ff" background="/outbox/images/background.gif">
  134. ENDOFTEXT
  135.  
  136.         &flush(STDOUT) ;
  137.     system '/var/www/cgi-bin/outbox-cgi', $PATH ;
  138.     &flush(STDOUT) ;
  139.     print <<ENDOFTEXT ;
  140. <B>$pathRoot:</B>
  141. ENDOFTEXT
  142. }
  143.  
  144. #
  145. # RFE - people getting confused with all these choices.
  146. #
  147. #  &ShowSelection ;
  148.  
  149. &ShowContents ;
  150.  
  151. print "\n</body>\n</html>\n\n" unless $arg == 1 ;
  152.  
  153.  
  154.  
  155. #__________________________________________________________
  156. #__________________________________________________________
  157. #__________________________________________________________
  158.  
  159.  
  160.  
  161. sub ErrBadPath
  162. {
  163.     print <<ENDOFTEXT ;
  164. Content-type: text/html
  165.  
  166. <HTML>
  167. <HEAD><TITLE>OutBox File Not Found</TITLE></HEAD>
  168. <BODY><H2>OutBox File Not Found</H2>
  169. The requested file "$PATH" was not found on this OutBox page.
  170. <P>
  171. ENDOFTEXT
  172.  
  173. if( defined $ENV{'HTTP_REFERER'} )
  174. {
  175.     $referer = $ENV{'HTTP_REFERER'} ;
  176.     print <<ENDOFTEXT ;
  177. <a href="$referer"><IMG SRC="/outbox/images/go-back.gif" BORDER=0 ALT="Back"></A>
  178. ENDOFTEXT
  179. }
  180.  
  181.         print <<ENDOFTEXT ;
  182. </BODY>
  183. </HTML>
  184. ENDOFTEXT
  185.  
  186.  
  187.     die ;
  188. }
  189.  
  190. #__________________________________________________________
  191.  
  192. sub DefaultMesg
  193. {
  194.     open(FILE, "/etc/passwd") ;
  195.     @passwd = <FILE> ;
  196.     close(FILE) ;
  197.  
  198.     unless( $arg == 1 )
  199.     {
  200.         print <<ENDOFTEXT ;
  201. Content-type: text/html
  202.  
  203. <head>
  204.     <title>
  205.         User List
  206.     </title>
  207. </head>
  208.  
  209. <body>
  210. <h1 align=center> 
  211.     User List
  212. </h1>
  213.  
  214. <hr size=3 width="50%" noshade>
  215. <p>
  216. <h3 align=center>
  217.     This site supports the following users:
  218. </h3>
  219. <p>
  220. <hr size=3 width="50%" noshade>
  221.  
  222. <ul>
  223. ENDOFTEXT
  224.     }
  225.  
  226.     print "<center>\n<table cellpadding=10>\n" ;
  227.  
  228.     open (FIND,"$ls $ROOT |") ;
  229.     while( <FIND> )
  230.     {
  231.         chop ;
  232.         next if $_ eq "\." ;
  233.         next if $_ eq "\.\." ;
  234.         next unless -d "$ROOT/$_" ;
  235.  
  236.         $name = "" ;
  237.  
  238.         foreach $line (@passwd)
  239.         {
  240.             @field = split(':', $line) ;
  241.             @_     = split(',', $field[4]) ;
  242.             ($name = $_[0]), last if $_ eq $field[0] ;
  243.         }
  244.  
  245.         print <<ENDOFTEXT ;
  246. <tr>
  247.     <th align=right>
  248.         <font size=+2>
  249.         <a href="$wrap/$_">$_</a>
  250.         </font>
  251.     </th>
  252.     <td>
  253.         <li>
  254.     </td>
  255.     <td>
  256.         <font size=+2>
  257.         <a href="$wrap/$_">$name</a>
  258.         </font>
  259.     </td>
  260. </tr>
  261. ENDOFTEXT
  262.     }
  263.     close(FIND) ;
  264.  
  265.     print "</table></center>\n</ul>\n</body>\n\n" ;
  266.  
  267.     die ;
  268. }
  269.  
  270. #__________________________________________________________
  271.  
  272. sub ValidPath
  273. {
  274.     return 1 unless /\.\./ ;
  275.     
  276.     return '' if /^\.\./ ;
  277.     return '' if /\/\.\.\// ;
  278.     return '' if /\.\.$/ ;
  279.  
  280.     return 1 ;
  281. }
  282.  
  283. #__________________________________________________________
  284.  
  285. sub HandleDownload
  286. {
  287.     print <<ENDOFTEXT ;
  288. Location: $PATH
  289.  
  290. ENDOFTEXT
  291.  
  292.     die ;
  293. }
  294.  
  295. #__________________________________________________________
  296.  
  297. sub ShowSelection
  298. {
  299.     local(@select) ;
  300.     
  301.     $FORM{'format'} = "default" unless defined $FORM{'format'} ;
  302.     $select{$FORM{'format'}} = "checked" ;
  303.  
  304.     print <<ENDOFTEXT ;
  305.  
  306. <center>
  307. <table cellpadding=2 cellspacing=0 border=0>
  308. <form action="$wrap$PATH" method=get>
  309. <tr>
  310.     <td><b>Viewing format:</b>
  311.         <input    type="radio"
  312.                 name="format"
  313.                 value="default"
  314.                 $select{'default'}> Default
  315.         <input    type="radio"
  316.                 name="format"
  317.                 value="long"
  318.                 $select{'long'}> Long
  319.         <input    type="radio"
  320.                 name="format"
  321.                 value="short"
  322.                 $select{'short'}> Short </th>
  323.     <td align=center>
  324.         <input type="submit" value="Apply"></td>    
  325. </tr>
  326. </form>
  327. </table>
  328. </center>
  329.  
  330. ENDOFTEXT
  331. }
  332.  
  333.  
  334. #__________________________________________________________
  335.  
  336. sub ShowContents
  337. {
  338.     local($format) = $FORM{'format'} ;
  339.  
  340.     #_________________________________
  341.     #
  342.     #    Get the directory and do
  343.     #    some sorting (directories)
  344.     #    come first, then files.
  345.     #_________________________________
  346.  
  347.     open (FIND,"$ls $ROOT$PATH |") ;
  348.     while( <FIND> )
  349.     {
  350.         chop ;
  351.         next if $_ eq "\." ;
  352.         next if ( $_ eq "\.\." && "/$pathRoot" eq  $PATH ) ;
  353.         next if ( /^\./ && $_ ne "\.\." ) ;
  354.         next if ( ! -r "$ROOT$PATH/$_" ) ;
  355.         next if ( -d "$ROOT$PATH/$_" && ! -x "$ROOT$PATH/$_" ) ;
  356.         push(@d, $_), next if -d "$ROOT$PATH/$_" ;
  357.         push(@f, $_) ;
  358.     }
  359.  
  360.     #_________________________________
  361.     #
  362.     #    Select method of display
  363.     #_________________________________
  364.  
  365.     &ListFolders(@d) ;
  366.  
  367.     $format = "default" if ! defined $format ;
  368.  
  369.     if( $format eq "long" )
  370.     {
  371.         &ListLong(@f) ;
  372.     }
  373.     elsif( $format eq "short" )
  374.     {
  375.         &ListShort(@f) ;
  376.     }
  377.     else        # default format
  378.     {
  379.         &ListDefault(@f) ;
  380.     }
  381. }
  382.  
  383. #__________________________________________________________
  384.  
  385. sub ListFolders
  386. {
  387.     local($size) = $#_ + 1 ;
  388.     local($i) ;
  389.     local($j) ;
  390.     local($index) ;
  391.     local($format) ;
  392.     local($cols) = 3 ;
  393.     local($rows) = $size / $cols ;
  394.     local($left) = $size % $cols ;
  395.     local($label) ;
  396.     local($image) ;
  397.  
  398.     return if $size == 0 ;
  399.  
  400.     #_________________________________
  401.  
  402.     $format = "?format=$FORM{'format'}" if defined $FORM{'format'} ;
  403.  
  404.     #_________________________________
  405.  
  406.     $rows = sprintf("%.0d", $rows) ;
  407.     $rows++ if $left > 0 ;
  408.     
  409.     print <<ENDOFTEXT ;
  410. </center>
  411. <table cellpadding=0 cellspacing=0 width="100%" border=0>
  412. ENDOFTEXT
  413.     LOOP:
  414.     for( $i = 0 ; $i < $rows ; $i++ )
  415.     {
  416.         print "<tr>\n" ;
  417.         for( $j = 0 ; $j < $cols ; $j++ )
  418.         {
  419.             $index = $i + $j * $rows ;
  420.  
  421.             if( $index >= $size )
  422.             {
  423.                 print "<td></td>\n" ;
  424.                 next ;
  425.             }
  426.             if ( $_[$index] eq ".." )
  427.             {
  428.                 $label = "" ;
  429.                 $image = "/outbox/images/go-back.gif" ;
  430.             }
  431.             else
  432.             {
  433.                 $label = "$_[$index]" ;
  434.                 $image = "/outbox/images/file_folder.gif" ;
  435.             }
  436.  
  437.             print <<ENDOFTEXT ;
  438. <td>
  439.     <a href="$wrap$PATH/$_[$index]/$format"><img src="$image" align=middle border=0></a>
  440. ENDOFTEXT
  441.                         if ( $label ne "" )
  442.             {
  443.                 print "  <a href=\"$wrap$PATH/$_[$index]/$format\">$label</a>" ;
  444.                 }
  445.             print "</td>"
  446.         }
  447.         print "</tr>\n" ;
  448.     }
  449.  
  450.     print <<ENDOFTEXT ;
  451. </table>
  452. ENDOFTEXT
  453. }
  454.  
  455. #__________________________________________________________
  456.  
  457. sub ListDefault
  458. {
  459.     local(@notes) ;
  460.     local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,@other) ;
  461.     local($USERPATH) = $PATH ;
  462.     local($first) = 1 ;
  463.     local($image) ;
  464.     local($url) ;
  465.  
  466.     $dltext = `/usr/bin/gettxt uxoutbox:22 "(download)"` ;
  467.  
  468.     return if ( $#_ == -1 ) ;
  469.  
  470.     substr($USERPATH, 0, 1) = "/~" ;
  471.  
  472.     print <<ENDOFTEXT ;
  473.  
  474. </center>
  475. <div align=left>
  476. ENDOFTEXT
  477.  
  478.     foreach (@_)
  479.     {
  480.         next if $_ eq "index\.html" ;
  481.         next if $_ eq "index\.html\.O" ;
  482.         next if $_ eq "index\.html\.N" ;
  483.         next if $_ eq "index\.shtml" ;
  484.         next if $_ eq "index\.shtml\.O" ;
  485.         next if $_ eq "index\.shtml\.N" ;
  486.         next if $_ eq "default\.gif" ;
  487.         next if ( /^\./ ) ;
  488.         next if ( /\.bak$/ ) ;
  489.         next if ( /\.sav$/ ) ;
  490.         next if ( /~$/ ) ;
  491.  
  492.         if ( /\.notes$/ )
  493.         {
  494.             $base = $_ ;
  495.             $base =~ s/\.notes$// ;
  496.             next if -r "$doc/$base" ;
  497.         }
  498.  
  499.         if ($first == 1)
  500.         {
  501.             $first = 0 ;
  502.             print "<HR ALIGN=LEFT WIDTH=\"80%\">\n" if ($#d >= 0) ;
  503.             print "<table cellpadding=0 cellspacing=1 border=0>" ;
  504.         }
  505.  
  506.         #_________________________________
  507.         #
  508.         #    Calculate the size of this
  509.         #    file
  510.         #_________________________________
  511.  
  512.         ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,@other) =
  513.             stat("$doc/$_") ; 
  514.  
  515.         if( $size == 1 )
  516.         {
  517.             $size = "<b>$size</b> byte" ;
  518.         }
  519.         elsif( $size >= 1024 )
  520.         {
  521.             $size = $size / 1024 ;
  522.             $size = sprintf("%.0d", $size) ;
  523.             $size = "<b>$size</b> KB" ;
  524.         }
  525.         else
  526.         {
  527.             $size = "<b>$size</b> bytes" ;
  528.         }
  529.  
  530.         #_________________________________
  531.         #
  532.         #    Add notes if applicable
  533.         #_________________________________
  534.  
  535.         if( -r "$doc/$_.notes" )
  536.         {
  537.             open(DATA, "$doc/$_.notes") ;
  538.             @notes = <DATA> ;
  539.             close(DATA) ;
  540.         }
  541.         
  542.         #_________________________________
  543.         #
  544.         # figure out which image to use
  545.         #_________________________________
  546.  
  547.         # defaults
  548.  
  549.         $image = "" ;
  550.         $url = "" ;
  551.  
  552.         # audio
  553.  
  554.         $image = "file_audio.gif" if ( /\.[aA][uU]$/ ) ;
  555.         $image = "file_audio.gif" if ( /\.[aA][iI][fF]$/ ) ;
  556.         $image = "file_audio.gif" if ( /\.aif[cf]$/ ) ;
  557.         $image = "file_audio.gif" if ( /\.[sS][nN][dD]$/ ) ;
  558.         $image = "file_audio.gif" if ( /\.[wW][aA][vV]$/ ) ;
  559.         $image = "file_audio.gif" if ( /\.[rR][aA]$/ ) ;
  560.         $image = "file_audio.gif" if ( /\.[rR][aA][mM]$/ ) ;
  561.         $image = "file_audio.gif" if ( /\.[aA][bB][sS]$/ ) ;
  562.         $image = "file_audio.gif" if ( /\.[mM][pP][2a]$/ ) ;
  563.         $image = "file_audio.gif" if ( /\.[mM][pP][2a][2a]$/ ) ;
  564.         $image = "file_audio.gif" if ( /\.[mM][pP][eE][gG][aA]$/ ) ;
  565.  
  566.         # image
  567.  
  568.         $image = "file_image.gif" if ( /\.[gG][iI][fF]$/ ) ;
  569.         $image = "file_image.gif" if ( /\.[iI][eE][fF]$/ ) ;
  570.         $image = "file_image.gif" if ( /\.tiff$/ ) ;
  571.         $image = "file_image.gif" if ( /\.[tT][iI][fF]$/ ) ;
  572.         $image = "file_image.gif" if ( /\.[jJ][pP][gG]$/ ) ;
  573.         $image = "file_image.gif" if ( /\.jpeg$/ ) ;
  574.         $image = "file_image.gif" if ( /\.[jJ][pP][eE]$/ ) ;
  575.         $image = "file_image.gif" if ( /\.rgb$/ ) ;
  576.         $image = "file_image.gif" if ( /\.ras$/ ) ;
  577.         $image = "file_image.gif" if ( /\.pcd$/ ) ;
  578.         $image = "file_image.gif" if ( /\.p[nbgp]m$/ ) ;
  579.         $image = "file_image.gif" if ( /\.x[bp]m$/ ) ;
  580.         $image = "file_image.gif" if ( /\.xwd$/ ) ;
  581.         $image = "file_image.gif" if ( /\.icon$/ ) ;
  582.         $image = "file_image.gif" if ( /\.fti$/ ) ;
  583.  
  584.         # video
  585.  
  586.         $image = "file_movie.gif" if ( /\.movie$/ ) ;
  587.         $image = "file_movie.gif" if ( /\.[mM][oO][vV]$/ ) ;
  588.         $image = "file_movie.gif" if ( /\.moov$/ ) ;
  589.         $image = "file_movie.gif" if ( /\.[mM][vV]$/ ) ;
  590.         $image = "file_movie.gif" if ( /\.[qQ][tT]$/ ) ;
  591.         $image = "file_movie.gif" if ( /\.[aA][vV][iI]$/ ) ;
  592.         $image = "file_movie.gif" if ( /\.[mM][pP][eEgGsSvV]$/ ) ;
  593.         $image = "file_movie.gif" if ( /\.[mM][pP][2sSvV][2sSvV]$/ ) ;
  594.         $image = "file_movie.gif" if ( /\.[mM][pP][eE][gG]$/ ) ;
  595.         $image = "file_movie.gif" if ( /\.[mM][pP][eE][gG][vV]$/ ) ;
  596.  
  597.         # html
  598.  
  599.         $image = "file_html.gif" if ( /\.[hH][tT][mM][lL]$/ ) ;
  600.         $image = "file_html.gif" if ( /\.[hH][tT][mM]$/ ) ;
  601.         $image = "file_html.gif" if ( /\.[sS][hH][tT][mM][lL]$/ ) ;
  602.  
  603.         # PostScript
  604.  
  605.         $image = "file_ps.gif" if ( /\.[pP][sS]$/ ) ;
  606.         $image = "file_ps.gif" if ( /\.[eE][pP][sS]$/ ) ;
  607.  
  608.         # text
  609.  
  610.         $image = "file_text.gif" if ( /\.[tT][xX][tT]$/ ) ;
  611.         $image = "file_text.gif" if ( /\.[rR][tT][xX]$/ ) ;
  612.         $image = "file_text.gif" if ( /\.[rR][tT][fF]$/ ) ;
  613.         $image = "file_text.gif" if ( /\.doc$/ ) ;
  614.         $image = "file_text.gif" if ( /\.fm$/ ) ;
  615.         $image = "file_text.gif" if ( /\.mif$/ ) ;
  616.  
  617.         # development
  618.  
  619.         $image = "file_makefile.gif" if ( /[Mm]akefile.*$/ ) ;
  620.         $image = "file_core.gif" if ( $_ eq "core" ) ;
  621.         $image = "file_h.gif" if ( /\.[hH]$/ ) ;
  622.         $image = "file_c.gif" if ( /\.c$/ ) ;
  623.         $image = "file_cxx.gif" if ( /\.[cC[xX][xX]$/ ) ;
  624.         $image = "file_cxx.gif" if ( /\.[cC]\+\+$/ ) ;
  625.         $image = "file_cxx.gif" if ( /\.C$/ ) ;
  626.         $image = "file_cxx.gif" if ( /\.[cC][cC]$/ ) ;
  627.         $image = "file_obj.gif" if ( /\.[oO]$/ ) ;
  628.         $image = "file_java.gif" if ( /\.java$/ ) ;
  629.         $image = "file_javaclass.gif" if ( /\.class$/ ) ;
  630.  
  631.         # showcase
  632.  
  633.         $image = "file_showcase.gif" if ( /\.sc$/ ) ;
  634.         $image = "file_showcase.gif" if ( /\.slides$/ ) ;
  635.         $image = "file_showcase.gif" if ( /\.sho$/ ) ;
  636.         $image = "file_showcase.gif" if ( /\.show$/ ) ;
  637.  
  638.         # command scripts
  639.  
  640.         $image = "file_shell.gif" if ( /\.pl$/ ) ;
  641.         $image = "file_shell.gif" if ( /\.sh$/ ) ;
  642.         $image = "file_shell.gif" if ( /\.csh$/ ) ;
  643.  
  644.         # DOS/Windows
  645.  
  646.         $image = "file_ms_exe.gif" if ( /\.[bB][aA][tT]$/ ) ;
  647.         $image = "file_ms_exe.gif" if ( /\.[eE][xX][eE]$/ ) ;
  648.         $image = "file_ms_exe.gif" if ( /\.[cC][oO][mM]$/ ) ;
  649.         $image = "file_ms_pp.gif" if ( /\.[pP][pP][tT]$/ ) ;
  650.         $image = "file_ms_access.gif" if ( /\.[aA][dD][bB]$/ ) ;
  651.         if ( /\.[xX][lL].$/ )
  652.         {
  653.             open(FILE, "$ROOT/$PATH/$_") ;
  654.             read(FILE,$buf,3);
  655.             $image = "file_ms_excel.gif"
  656.             if (substr($buf,0,2) eq "\xd0\xcf" ||
  657.                 substr($buf,0,1) eq "\x09" &&
  658.                 substr($buf,2,1) eq "\x06") ;
  659.             close(FILE);
  660.         }
  661.         if ( /\.[dD][oO][cCtT]$/ )
  662.         {
  663.             open(FILE, "$ROOT/$PATH/$_") ;
  664.             read(FILE,$buf,2);
  665.             $image = "file_ms_word.gif"
  666.             if ($buf eq "\xfe\x37" ||
  667.                 $buf eq "\xdb\xa5" ||
  668.                 $buf eq "\xd0\xcf") ;
  669.             close(FILE);
  670.         }
  671.         if ( /\.[fF][mM]$/ )
  672.         {
  673.             open(FILE, "$ROOT/$PATH/$_") ;
  674.             read(FILE,$buf,4);
  675.             $image = "file_claris_fm.gif"
  676.             if ($buf eq "\x00\x01\x00\x00" ||
  677.                 $buf eq "\x00\x02\x00\x00") ;
  678.             close(FILE);
  679.         }
  680.  
  681.  
  682.         # other
  683.  
  684.         $image = "file_iv.gif" if ( /\.iv$/ ) ;
  685.         $image = "file_iv.gif" if ( /\.ano$/ ) ;
  686.         $image = "file_tar.gif" if ( /\.[tT][aA][rR]$/ ) ;
  687.         $image = "file_compress.gif" if ( /\.[gG][zZ]$/ ) ;
  688.         $image = "file_compress.gif" if ( /\.[tT][gG][zZ]$/ ) ;
  689.         $image = "file_compress.gif" if ( /\.Z$/ ) ;
  690.         $image = "file_vrml.gif" if ( /\.vrml$/ ) ;
  691.         $image = "file_vrml.gif" if ( /\.vrml\.gz$/ ) ;
  692.         $image = "file_vrml.gif" if ( /\.wrl$/ ) ;
  693.         $image = "file_vrml.gif" if ( /\.wrl\.gz$/ ) ;
  694.         $image = "file_midi.gif" if ( /\.[mM][iI][dD]$/ ) ;
  695.         $image = "file_midi.gif" if ( /\.[mM][iI[dD][iI]$/ ) ;
  696.         $image = "file_pdf.gif" if ( /\.[pP][dD][fF]$/ ) ;
  697.         $image = "file_director.gif" if ( /\.[dD][cCxXiI][rR]$/ ) ;
  698.  
  699.         # generic ms-windows application data
  700.                 # (i.e. has suffix, no types above match)
  701.         if ( $image eq "" && /..*\...*$/ )
  702.         {
  703.             open(FILE, "$ROOT/$PATH/$_") ;
  704.             read(FILE,$buf,16);
  705.             $image = "file_ms_generic.gif"
  706.             if ($buf eq "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1\x00\x00\x00\x00\x00\x00\x00\x00" ||
  707.                 $buf eq "\x09\x04\x06\x00\x00\x00\x10\x00\xf6\x05\x5c\x00\x20\x00\x03\x2e") ;
  708.             close(FILE);
  709.         }
  710.  
  711.         # types without suffix
  712.         
  713.         if ( $image eq "" )
  714.         {
  715.             open(FILE, "$ROOT/$PATH/$_") ;
  716.             if ( $string = <FILE> )
  717.             {
  718.             # WebJumper
  719.             if ( $string =~ /^\#SGIWebJumpsite/ )
  720.             {
  721.                 $image = "file_webjumper.gif" ;
  722.                 while ( $string = <FILE> )
  723.                 {
  724.                 $string =~ s/\n$// ;
  725.  
  726.                 # look for ://
  727.                 if ( $string =~ /:\/\// )
  728.                 {
  729.                     # don't view it, go to it.
  730.                     $url = $string ;
  731.                     last ;
  732.                 }
  733.                 else
  734.                 {
  735.                     # use as a note, if no .notes file
  736.                     @notes = ( $string )
  737.                     if ( @notes == 0 &&
  738.                          $string ne "URL Pasted into Desktop" ) ;
  739.                 }
  740.                 }
  741.             }
  742.  
  743.             # command script
  744.             $image = "file_shell.gif" if ( $string =~ /^\#!\/bin\/.*sh/ ) ;
  745.             $image = "file_shell.gif" if ( $string =~ /^\#!\/usr\/s*bin\/perl/ ) ;
  746.             }
  747.             close(FILE) ;
  748.         }
  749.  
  750.         # generic executable
  751.         
  752.         if ( $image eq "" )
  753.         {
  754.             $image = "file_exec.gif" if ( -x "$ROOT/$PATH/$_" ) ;
  755.         }
  756.  
  757.         # default to generic data file icon
  758.  
  759.         $image = "file_data.gif" if ( $image eq "" ) ;
  760.  
  761.         # default to view data
  762.  
  763.         $url = "$USERPATH/$_" if ( $url eq "" ) ;
  764.  
  765.         #_________________________________
  766.         #
  767.         #    Diplay the data in table form
  768.         #_________________________________
  769.  
  770.         print <<ENDOFTEXT ;
  771. <tr>
  772.     <td>
  773.         <a href="$url"><img src="/outbox/images/$image" align=middle border=0></a>
  774.     </td>
  775.     <td>
  776.         <a href="$url">$_</a>
  777.     </td>
  778.     <td>
  779.         $size
  780.     </td>
  781.     <td align=left>
  782.         <a href="/cgi-bin/handler$PATH/$_?data=Download">$dltext</a>
  783.     </td>
  784. ENDOFTEXT
  785.  
  786.         #_________________________________
  787.         #
  788.         #    If there is a .notes file,
  789.         #    show the message within the
  790.         #    HTML doc.
  791.         #_________________________________
  792.  
  793.         if ( @notes > 0 )
  794.         {
  795.             print "<td>" ;
  796.             foreach (@notes)
  797.             {
  798.                 print ;
  799.             }
  800.             print "</td>\n" ;
  801.             @notes = () ;
  802.         }
  803.         print "</tr>\n" ;
  804.     }
  805.     print "</table></center>\n" if $first == 0 ;
  806. }
  807.  
  808. #__________________________________________________________
  809.  
  810. sub ListShort
  811. {
  812.     local($size) = $#_ + 1 ;
  813.     local($i) ;
  814.     local($j) ;
  815.     local($index) ;
  816.     local($cols) = 3 ;
  817.     local($rows) = $size / $cols ;
  818.     local($left) = $size % $cols ;
  819.  
  820.     return if $size == 0 ;
  821.  
  822.     $rows = sprintf("%.0d", $rows) ;
  823.     $rows++ if $left > 0 ;
  824.     
  825.     print <<ENDOFTEXT ;
  826.  
  827. </center>
  828. <h3>Shared Files:</h3>
  829.  
  830. <table cellpadding=5 cellspacing=3 border width="100%"><tr><td>
  831. <table cellpadding=3 width="100%">
  832. ENDOFTEXT
  833.     LOOP:
  834.     for( $i = 0 ; $i < $rows ; $i++ )
  835.     {
  836.         print "<tr>\n" ;
  837.         for( $j = 0 ; $j < $cols ; $j++ )
  838.         {
  839.             $index = $i + $j * $rows ;
  840.             if( $index >= $size )
  841.             {
  842.                 print "<td></td>\n" ;
  843.                 next ;
  844.             }
  845.             print <<ENDOFTEXT ;
  846. <td>
  847.     <a href="$PATH/$_[$index]">
  848.         <img src="/outbox/images/file_data.gif" align=middle border=0>  
  849.         $_[$index]
  850.     </a>
  851. </td>
  852. ENDOFTEXT
  853.         }
  854.         print "</tr>\n" ;
  855.     }
  856.  
  857.     print <<ENDOFTEXT ;
  858. </table>
  859. </td></tr></table>
  860. ENDOFTEXT
  861. }
  862.  
  863. #__________________________________________________________
  864.  
  865. sub ListLong
  866. {
  867.     local($unit) ;
  868.     local(@other) ;
  869.     local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) ;
  870.     local($atime, $mtime, $ctime) ;
  871.     local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) ;
  872.     
  873.     return if $#_ == -1 ;
  874.     
  875.     print <<ENDOFTEXT ;
  876.  
  877. <h3>Shared Files:</h3>
  878. <table cellpadding=5 cellspacing=3 border width="100%">
  879. <tr><td>
  880. <pre>
  881.  
  882. ENDOFTEXT
  883.  
  884.     foreach (@_)
  885.     {
  886.         #_________________________________
  887.         #
  888.         #    stat document and get the 
  889.         #    pertinent info on it.  Also
  890.         #    get a unit for the size, ie
  891.         #    bytes or Kb, etc
  892.         #_________________________________
  893.         
  894.         ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  895.             $atime, $mtime, $ctime, @other) =
  896.             stat("$doc/$_") ; 
  897.  
  898.         if( $size == 1 )
  899.         {
  900.             $unit = "byte" ;
  901.         }
  902.         elsif( $size >= 1024 )
  903.         {
  904.             $size = $size / 1024 ;
  905.             $size = sprintf("%.0d", $size) ;
  906.             $unit = "KB" ;
  907.         }
  908.         else
  909.         {
  910.             $unit = "bytes" ;
  911.         }
  912.  
  913.         #_________________________________
  914.         #
  915.         #    Get last mod time
  916.         #_________________________________
  917.  
  918.         ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,@other) = 
  919.             localtime($ctime) ;
  920.         $theDay   = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[$wday] ;
  921.         $theMonth = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon] ;
  922.         $date     = sprintf("$theDay $theMonth $mday %02d:%02d:%02d 19$year",
  923.                             $hour, $min, $sec) ;
  924.  
  925.         #_________________________________
  926.  
  927.         local($filler) = " " x (25 - length($_)) ;
  928.         $filler = 0 if $filler < 0 ;
  929.         print sprintf("<a href=\"$PATH/$_\"><img src=\"/outbox/images/file_data.gif\" align=middle border=0>  %.25s</a>%s %6d %-5.5s %s\n", $_, $filler, $size, $unit, $date) ;
  930.     }
  931.  
  932.     print <<ENDOFTEXT ;
  933. </pre>
  934. </td></tr>
  935. </table>
  936. ENDOFTEXT
  937. }
  938.  
  939.